package com.xsmart.oa.services;


import com.baomidou.mybatisplus.plugins.Page;
import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.oa.dao.SystemPermissionListDAO;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.*;

@Service
public class SystemPermissionListService {
    @Autowired
    SystemPermissionListDAO systemPermissionListDAO;
    /*
     * 查询
     * */
    public ResponseBean querySystemPermissionList(RequestBean requestBean){
        int currentPage = requestBean.getRequestParamInteger("currentPage",1);
        int pageSize = requestBean.getRequestParamInteger("pageSize",10);
        String action_code = requestBean.getRequestParamString("action_code");
        String action_desc = requestBean.getRequestParamString("action_desc");
        Map<String,Object> systemPermissionListMap = new HashMap<>();
        systemPermissionListMap.put("action_code",action_code);
        systemPermissionListMap.put("action_desc",action_desc);
        ResponseBean responseBean = requestBean.getResponseBean();
        Page<Map<String,Object>> page = new Page<>(currentPage,pageSize);
        List<Map<String, Object>> list = new ArrayList<>();
        if (action_code!=null || action_desc!=null){
            list = systemPermissionListDAO.querySystemPermissionListSecond(page, systemPermissionListMap);
            Map<String, Object> listMap = new HashMap<>();
            getSystemPermissionListChildren3(list,null);
            if(list.size()>0){
                Iterator<Map<String, Object>> itList = list.listIterator();
                listMap = itList.next();
                String parentNode=queryParentNode(MapUtils.getInteger(listMap,"ACTION_ID"),null);
                listMap.put("parent_node",parentNode);
                list.clear();
                list.add(listMap);
            }
//            getSystemPermissionListChildren(page.getRecords());

        }else{
            list = systemPermissionListDAO.querySystemPermissionList(page, systemPermissionListMap);
            getSystemPermissionListChildren(list);
        }
        page.setRecords(list);
        responseBean.addResultPageItem("systemPermissionList",page);
        responseBean.setResult(ResultConstant.SUCCESS);
        return  responseBean;
    }
    private void getSystemPermissionListChildren3(List<Map<String, Object>> list, Map<String, Object> parent) {
        for (Map<String, Object> systemPermissionInfo : list) {
//            if (parent != null && parent.containsKey("parent_node")) {
//                systemPermissionInfo.put("parent_node", MapUtils.getString(parent, "parent_node", "") + "," + MapUtils.getString(systemPermissionInfo, "PARENT_ACTION_ID", ""));
//            } else {
//                systemPermissionInfo.put("parent_node", MapUtils.getString(systemPermissionInfo, "PARENT_ACTION_ID", ""));
//            }
            String parentsNode=queryParentNode(MapUtils.getInteger(systemPermissionInfo,"ACTION_ID"),null);
            systemPermissionInfo.put("parent_node",parentsNode);
            System.out.println(systemPermissionInfo);

            List<Map<String, Object>> childrenList = systemPermissionListDAO.querySystemPermissionSubtree(MapUtils.getInteger(systemPermissionInfo,"ACTION_ID"));
            if (childrenList.size() > 0) {
                systemPermissionInfo.put("children", childrenList);
                getSystemPermissionListChildren3(childrenList, systemPermissionInfo);
            }
        }
    }


    /*
     * 插入
     * */
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean addSystemPermissionList(RequestBean requestBean){
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer parent_action_id = requestBean.getRequestParamInteger("parent_action_id");
        String action_code = requestBean.getRequestParamString("ACTION_CODE");
        String action_desc = requestBean.getRequestParamString("ACTION_DESC");
        Integer action_type = requestBean.getRequestParamInteger("action_type");
        Map<String, Object> systemPermissionListMap = new HashMap<>();
        systemPermissionListMap.put("PARENT_ACTION_ID",parent_action_id);
        systemPermissionListMap.put("ACTION_CODE",action_code);
        systemPermissionListMap.put("ACTION_DESC",action_desc);
        systemPermissionListMap.put("action_type",action_type);
        try{
            int num = systemPermissionListDAO.addSystemPermissionList(systemPermissionListMap);
            responseBean.setResult(ResultConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
            throw new RuntimeException();
        }

        return responseBean;
    }
    /*
     * 修改
     * */
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean modSystemPermissionList(RequestBean requestBean){
        ResponseBean responseBean=requestBean.getResponseBean();
        Integer action_id=requestBean.getRequestParamInteger("action_id");
        Integer parent_action_id=requestBean.getRequestParamInteger("parent_action_id");
        String action_code=requestBean.getRequestParamString("ACTION_CODE");
        String action_desc=requestBean.getRequestParamString("ACTION_DESC");
        Integer action_type=requestBean.getRequestParamInteger("action_type");
        Map<String,Object> systemPermissionListMap=new HashMap<>();
        systemPermissionListMap.put("ACTION_ID",action_id);
        systemPermissionListMap.put("PARENT_ACTION_ID",parent_action_id);
        systemPermissionListMap.put("ACTION_CODE",action_code);
        systemPermissionListMap.put("ACTION_DESC",action_desc);
        systemPermissionListMap.put("action_type",action_type);
        try{
            int num=systemPermissionListDAO.modSystemPermissionList(systemPermissionListMap);
            responseBean.setResult(ResultConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
            throw new RuntimeException();
        }

        return responseBean;
    }
    /*
     * 删除
     * */
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean delSystemPermissionList(RequestBean requestBean){
        ResponseBean responseBean=requestBean.getResponseBean();
        Integer action_id=requestBean.getRequestParamInteger("action_id");
        try {
            delChildNodeList(action_id);
            responseBean.setResult(ResultConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
            throw new RuntimeException();
        }

        return responseBean;
    }


    public ResponseBean findParentNode(RequestBean requestBean){
        Integer actionId=requestBean.getRequestParamInteger("action_id");
        System.out.println("actionId是"+actionId);
        Map<String,Object> parentNodeMap=new HashMap<>();
        parentNodeMap.put("action_id",actionId);
        ResponseBean responseBean=requestBean.getResponseBean();
        List<Map<String,Object>> parentNodeList=new ArrayList<>();
        Map<String,Object> rootNode=new HashMap<>();
        rootNode.put("value",1);
        rootNode.put("label","无父类权限");
        parentNodeList.add(rootNode);
        List<Map<String,Object>> parentNode=systemPermissionListDAO.querySystemPermissionSubtree(null);
        getNodeChildren(parentNode,actionId);
        rootNode.put("children",parentNode);
        responseBean.addResultItem("parentList",parentNodeList);
        System.out.println("parentList是"+parentNodeList);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    private String queryParentNode(Integer actionId, String parentNode) {
        parentNode=parentNode==null?String.valueOf(actionId):actionId+","+parentNode;
        Map<String, Object> pList = systemPermissionListDAO.queryPsystemPermission(actionId);
        int parentId=MapUtils.getIntValue(pList,"PARENT_ACTION_ID",1);
        if(parentId==1){
            parentNode = "1" + "," + parentNode;
            return parentNode;
        }else{
            return queryParentNode(parentId,parentNode);
        }
    }




    /*
     * 获取下拉框级联数据
     * */
    private void getNodeChildren(List<Map<String, Object>> list, Integer actionId) {
        Iterator<Map<String, Object>> itList = list.listIterator();
        while (itList.hasNext()) {
            Map<String, Object> parent = itList.next();
            Integer aactionId = MapUtils.getInteger(parent, "ACTION_ID");
            if (aactionId.equals(actionId)) {
                itList.remove();
            } else {
                String actionCode = MapUtils.getString(parent, "ACTION_CODE");
                parent.clear();
                parent.put("label", actionCode);
                parent.put("value", aactionId);
                List<Map<String, Object>> childrenList = systemPermissionListDAO.querySystemPermissionSubtree(aactionId);
                if (childrenList.size() > 0) {
                    parent.put("children", childrenList);
                    getNodeChildren(childrenList, actionId);
                }
            }
        }
    }

    /*
     * 获取子类
     * */
    private void getSystemPermissionListChildren(List<Map<String, Object>> list) {
        getSystemPermissionListChildren(list, null);
    }

    private void getSystemPermissionListChildren(List<Map<String, Object>> list, Map<String, Object> parent) {
        for (Map<String, Object> systemPermissionInfo : list) {
            if (parent != null && parent.containsKey("parent_node")) {
                systemPermissionInfo.put("parent_node", MapUtils.getString(parent, "parent_node", "") + "," + MapUtils.getString(systemPermissionInfo, "PARENT_ACTION_ID", ""));
            } else {
                systemPermissionInfo.put("parent_node", MapUtils.getString(systemPermissionInfo, "PARENT_ACTION_ID", ""));
            }

            List<Map<String, Object>> childrenList = systemPermissionListDAO.querySystemPermissionSubtree(MapUtils.getInteger(systemPermissionInfo,"ACTION_ID"));
            if (childrenList.size() > 0) {
                systemPermissionInfo.put("children", childrenList);
                getSystemPermissionListChildren(childrenList, systemPermissionInfo);
            }
        }
    }

    /*
    * 级联删除
    * */

    private void delChildNodeList(Integer actionId){
        List<Map<String,Object>> list = systemPermissionListDAO.queryChildNodeList(actionId);
        if (list.size()>0){
            for (Map<String,Object> map :list){
                Integer actionChildrenId=MapUtils.getInteger(map,"action_id");
                delChildNodeList(actionChildrenId);
            }
        }
        systemPermissionListDAO.delSystemPermissionList(actionId);
    }
}
